import time

import pandas as pd
from flask import Flask, render_template, jsonify
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Pie, Map, Grid

import untils

app = Flask(__name__)


@app.route('/')
def reBar():
    # line = (
    #     Line()
    #         .add_xaxis(list(df['province'].values))
    #         .add_yaxis("治愈", df['heal'].values.tolist())
    #         .add_yaxis("死亡", df['dead'].values.tolist())
    #         .set_series_opts(label_opts=opts.LabelOpts(color="white"))
    #         .set_global_opts(
    #         title_opts=opts.TitleOpts(title="死亡与治愈", title_textstyle_opts=opts.TextStyleOpts(color="white")),
    #         yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12, color="white")),
    #         xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12, color="green")),
    #         legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="white"))
    #     )
    # )
    #
    # bar = (
    #     Bar(init_opts=opts.InitOpts(theme='dark'))
    #
    #         .add_xaxis(list(df['province'].values)[:6])
    #         .add_yaxis("死亡", df['dead'].values.tolist()[:6])
    #         .add_yaxis("治愈", df['heal'].values.tolist()[:6])
    #         .set_series_opts(label_opts=opts.LabelOpts(color="white"))
    #         .set_global_opts(
    #         title_opts=opts.TitleOpts(title="各地区确诊人数与死亡人数",
    #                                   title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=13)),
    #         datazoom_opts=[opts.DataZoomOpts()],
    #         yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12, color="white")),
    #         xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12, color="green")),
    #         legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="white"))
    #     )
    #
    # )

    untils.paqu_data()
    untils.yiqing_area()
    untils.insert_data("yiqing.csv", "highlist.csv", "middlelist.csv", "count.csv")
    untils.insert_hainanzhudao()

    return render_template("main.html", bar_data=untils.get_bar(), line_data=untils.get_line(),
                           map_data=untils.get_map(), pie_data=untils.get_pie())


@app.route('/time')
def get_time():
    return untils.gettime()


@app.route('/shuju')
def get_shuju():
    data = untils.get_all_data()
    return jsonify({"confirm": data[0], "dead": data[1], "heal": data[2], "wzz": data[3]})


@app.route("/r2")
def get_r2_data():
    data = untils.get_r2_data()

    # end_update_time, province, city, county, address, type
    details = []
    risk = []
    end_update_time = data[0][0]

    for a, b, c, d, e, f in data:
        risk.append(f)
        details.append(f"{b}\t{c}\t{d}\t{e}")
    return jsonify({"update_time": end_update_time, "details": details, "risk": risk})


@app.route('/r22')
def get_count():
    data = untils.get_count()
    account1 = data[0][1]
    account2 = data[0][2]
    return jsonify({"account1": account1, "account2": account2})


if __name__ == '__main__':
    app.run()
